/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-13 20:09:08
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-10-30 00:38:42
 */

import {
  extraItemPage,
  extraItemList,
  extraItemAdd,
  extraItemEdit,
  extraItemDetail,
  extraItemDelete,
} from 'services/extraItem';

export default {
  namespace: 'extraItemList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    extraItemList: [],
    addExtraItemList: [],
    detailData: {},
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(extraItemPage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },

    *getExtraItemList({ payload, callback }, { call, put }) {
      const response = yield call(extraItemList, payload);
      yield put({
        type: 'saveGetExtraItemList',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },

    *deleteDetailData({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDeleteDetailData',
      });
      if (callback) callback();
    },

    *addExtraItemList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveAddExtraItemList',
        payload: payload,
      });
    },

    *deleteExtraItemList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDeleteExtraItemList',
        payload: payload,
      });
    },

    *clearExtraItemList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveClearExtraItemList',
      });
    },

    *doAddExtraItemList({ payload, callback }, { call, put }) {
      const response = yield call(extraItemAdd, payload);
      if (callback) callback(response);
    },
    *doExtraItemEdit({ payload, callback }, { call, put }) {
      const response = yield call(extraItemEdit, payload);
      if (callback) callback(response);
    },

    // 新增套餐加项--修改
    *editExtraItemList({ payload }, { call, put }) {
      yield put({
        type: 'saveExtraItemList',
        payload,
      });
    },

    *extraItemDetail({ payload, callback }, { call, put }) {
      const response = yield call(extraItemDetail, payload);
      yield put({
        type: 'saveExtraItemDetail',
        payload: response,
      });
    },

    *extraItemDelete({ payload, callback }, { call, put }) {
      const response = yield call(extraItemDelete, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { extraItemList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = extraItemList.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
    saveGetExtraItemList(state, action) {
      const { data } = action.payload;

      if (!data) {
        return {
          ...state,
        };
      }

      const newList = data.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        extraItemList: newList,
      };
    },

    saveDeleteDetailData(state) {
      return {
        ...state,
        detailData: {},
      };
    },

    saveAddExtraItemList(state, action) {
      const { addExtraItemList } = state;
      addExtraItemList.push({
        key: addExtraItemList.length + 1,
        type: 'add',
        description: '',
        name: '',
        price: '',
      });
      return {
        ...state,
        addExtraItemList,
      };
    },

    saveDeleteExtraItemList(state, action) {
      const { addExtraItemList } = state;
      const { key } = action.payload;
      const newList = addExtraItemList.filter(v => {
        return v.key != key;
      });

      return {
        ...state,
        addExtraItemList: newList,
      };
    },

    saveClearExtraItemList(state, action) {
      return {
        ...state,
        addExtraItemList: [],
      };
    },

    saveExtraItemList(state, action) {
      const { addExtraItemList } = state;
      const newList = addExtraItemList.map(val => {
        const v = val;
        let v1 = v;
        if (v.key === action.payload.key) {
          v1 = { ...v, ...action.payload };
        }
        return v1;
      });
      return {
        ...state,
        addExtraItemList: newList,
      };
    },

    saveExtraItemDetail(state, action) {
      const { data } = action.payload;
      if (data.storeFeatures) {
        data.storeFeatures = data.storeFeatures.split(',').map(v => {
          return v;
        });
      }
      if (data.extraItemList) {
        data.extraItemList = data.extraItemList.map(v => {
          return v.id;
        });
      }

      if (data.extraItemDetailList) {
        data.extraItemDetailList = data.extraItemDetailList.map((v, i) => {
          v.key = i;
          return v;
        });
      } else {
        data.extraItemDetailList = [];
      }

      return {
        ...state,
        detailData: data,
        addExtraItemList: data.extraItemDetailList,
      };
    },
  },
};
